﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.LyDoCamThiService;
namespace eTMS.Controllers{
public class LyDoCamThiControllerBase : CommonController{
	public LyDoCamThiControllerBase()
		: base()
	{	
		this.dao = new LyDoCamThiServiceClient();
	}
	~LyDoCamThiControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private LyDoCamThiEntity oLyDoCamThi;	
	public LyDoCamThiEntity LyDoCamThiObject{
		get{
			return oLyDoCamThi;
		}
		set{
			oLyDoCamThi=value;
		}
	}
	protected LyDoCamThiServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSLyDoCamThi(false);			
			return _Table;
		}
	}
	public void LoadAllDSLyDoCamThi(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oLyDoCamThi==null)return false;
		bool result=true;
		try{
			oLyDoCamThi = dao.SaveRet(oLyDoCamThi);
		}
		catch{
			result=false;
		}
		if(result){
			_ListLyDoCamThi.Add(oLyDoCamThi);
		}
		return result;
	}
	public bool Update(){
		if(oLyDoCamThi==null)return false;
		return dao.Update(oLyDoCamThi);
	}
	public bool Delete(){
		if(oLyDoCamThi==null)return false;
		bool retvalue = dao.Delete(oLyDoCamThi);
		if(retvalue){			
			_ListLyDoCamThi.Remove(oLyDoCamThi);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 LyDoCamThi_ID){
		bool retvalue = dao.DeleteByID(LyDoCamThi_ID);		
		if(retvalue){
			LyDoCamThiEntity oDeleted=FindLyDoCamThi(LyDoCamThi_ID);
			if(oDeleted!=null){
				_ListLyDoCamThi.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oLyDoCamThi = new LyDoCamThiEntity();
	}   
    //Copy data from data row in grid to object
    public LyDoCamThiEntity MapDataRowToObject(DataRow row){
		LyDoCamThiEntity o = new LyDoCamThiEntity();
		 if(!row.IsNull("LyDoCamThi_ID"))
			o.LyDoCamThi_ID=ConvertInt(row["LyDoCamThi_ID"]);
		 o.TenLyDo=Convert.ToString(row["TenLyDo"]);
		 o.GhiChu=Convert.ToString(row["GhiChu"]);
		 if(!row.IsNull("LyDoCamThi_ID"))
		    o.LyDoCamThi_ID=(Int32)(row["LyDoCamThi_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(LyDoCamThiEntity o,DataRow row){
		row["LyDoCamThi_ID"]=o.LyDoCamThi_ID;
		row["TenLyDo"]=o.TenLyDo;
		row["GhiChu"]=o.GhiChu;
		row["LyDoCamThi_ID"]=o.LyDoCamThi_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.LyDoCamThiObject,row);
    }
	public void LoadObject(Int32 LyDoCamThi_ID){
		LyDoCamThiEntity oLoad=FindLyDoCamThi(LyDoCamThi_ID);
		if(oLoad==null){
			oLyDoCamThi=this.LoadLyDoCamThi(LyDoCamThi_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oLyDoCamThi.IsNullLyDoCamThi_ID;
			if(!bNotIsNull){
				this.ListLyDoCamThi.Add(oLyDoCamThi);
			}
		}
		else{
			oLyDoCamThi=oLoad;
		}
	}
	#region CachedList LyDoCamThi
	private static List<LyDoCamThiEntity> _ListLyDoCamThi=new List<LyDoCamThiEntity>();
    public List<LyDoCamThiEntity>ListLyDoCamThi{
		get{
			return _ListLyDoCamThi;
		}
	}
	public static List<LyDoCamThiEntity>ListLyDoCamThiStatic{
		get{
			return _ListLyDoCamThi;
		}
	}
	public LyDoCamThiEntity FindLyDoCamThi(Int32 LyDoCamThi_ID){
		LyDoCamThiEntity oResult=this.ListLyDoCamThi.Find(delegate(LyDoCamThiEntity oFind){
			bool result=true; 
			result&=oFind.LyDoCamThi_ID.Equals(LyDoCamThi_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllLyDoCamThi(bool clearCache){
		if(_ListLyDoCamThi.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		LyDoCamThiServiceClient dao = new LyDoCamThiServiceClient();
		_ListLyDoCamThi=new List<LyDoCamThiEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public LyDoCamThiEntity LoadLyDoCamThi(Int32 LyDoCamThi_ID){			
		LyDoCamThiEntity o=dao.SelectOne(LyDoCamThi_ID);		
		return o;
	}
	#endregion
}
}
